package com.s3.khospitalds.controller;

import com.s3.khospitalds.common.R;
import com.s3.khospitalds.entity.Patientlist;
import com.s3.khospitalds.entity.Prescription;
import com.s3.khospitalds.entity.PrintBill;
import com.s3.khospitalds.entity.Prescription;
import com.s3.khospitalds.service.PrescriptionService;
import com.s3.khospitalds.service.impl.PatientlistServiceImpl;
import com.s3.khospitalds.service.impl.PrescriptionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author: xw
 * @CreateTime: 2025/2/25
 * @Description: 患者药方表
 * @Version: 1.0
 */
@RestController
@CrossOrigin(origins = "*")
public class PrescriptionController {
    @Autowired
    PrescriptionServiceImpl prescriptionService;
    @Autowired
    PatientlistServiceImpl patientlistService;

    @GetMapping("/getAllPrescription")
    public R getAllPrescription() {
        return new R().addData("Prescription",prescriptionService.selectAll());
    }

    @GetMapping("/findPrescriptionByPatientNumber/{num}")
    public R findPrescriptionByPatientNumber(@PathVariable("num") String patientNumber) {//根据编号查询处方
        return new R().addData("Prescription", prescriptionService.findPrescriptionByPatientNumber(patientNumber));
    }

    @GetMapping("/findPrescriptionByPatientNumberAndSun/{num}")
    public R findPrescriptionByPatientNumberAndSun(@PathVariable("num") String patientNumber) {//根据编号查询处方然后计算总价
        double price =0;
        List<Prescription> prescriptionByPatientNumber = prescriptionService.findPrescriptionByPatientNumber(patientNumber);
        for (int i = 0; i < prescriptionByPatientNumber.size(); i++) {
            price += prescriptionByPatientNumber.get(i).getPrice();
        }
        patientlistService.updateTotalPrice(price,patientNumber);
        return new R().addData("price", price);
    }
    @GetMapping("/selectPrescriptionByNumber/{num}/{price}")
    public R selectPrescriptionByNumber(@PathVariable("num") String num,@PathVariable("price") String price) {
        R r = new R();
        List<PrintBill> printBills=new ArrayList<>();
        List<Prescription> prescriptionByPatientNumber = prescriptionService.findPrescriptionByPatientNumber(num);
        List<Patientlist> patientlists = patientlistService.selectPatientNumber(num);
        for (int i = 0; i < prescriptionByPatientNumber.size(); i++) {
            PrintBill printBill = new PrintBill();
            printBill.setName(patientlists.get(0).getName());
            printBill.setSex(patientlists.get(0).getSex());
            printBill.setDoctor(patientlists.get(0).getDoctor());
            printBill.setDrugName(prescriptionByPatientNumber.get(i).getDrugName());
            printBill.setUsage(prescriptionByPatientNumber.get(i).getRemark());
            printBill.setType(prescriptionByPatientNumber.get(i).getType());
            printBill.setPrice(prescriptionByPatientNumber.get(i).getPrice());
            printBills.add(printBill);
        }
        printBills.get(0).setPriceAll(price);
        r.addData("printBills",printBills);
        return r;
    }

    @PostMapping("/insertPrescriptions")
    public boolean saveBatch(@RequestBody ArrayList<Prescription> prescriptions) {
        if (prescriptions == null) {
            return false;
        } else if (prescriptions.isEmpty()) {
            return false;
        }
        return prescriptionService.saveBatch(prescriptions);
    }



}
